package net.thxjj.adminServer.entity.onenet;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.UnsupportedEncodingException;

@Document(collection="th_one_net_log")//集合名
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class OnetEvent {
    private long createTime;
    private String eventContent;
    private String deviceId;


    public OnenetTransResultBean transToBean(String eventContent){

        OnenetTransResultBean onenetTransResultBean = new OnenetTransResultBean();

        if(StringUtils.isEmpty(eventContent) || eventContent.length()<=4){
            onenetTransResultBean.setEventType(0);
            return onenetTransResultBean;
        }
        try {
                String content = eventContent;
                if(eventContent.startsWith("30")){
                    content = new String(ByteUtls.hexStr2Bytes(eventContent),"GBK");
                }

                int content_total = content.length();
                String leng = content.substring(0,4);
                int da = Integer.parseInt(leng,16);
                if(da!=(content_total - 4)/2 || content_total%2!=0){
                    onenetTransResultBean.setEventType(0);
                }

                String type = content.substring(10,14);

                if("1401".equals(type)){
                        onenetTransResultBean.setEventType(1);
                        String packagea = content.substring(10);
                        String device_status_str = packagea.substring(4,6);
                        String smoke_value_str = packagea.substring(6,8);
                        String battery_value_str = packagea.substring(8,10);
                        String temp_str = packagea.substring(10,12);
                        String csq_str = packagea.substring(12,14);
                        String ecl_str = packagea.substring(14,16);
                        String snr_str = packagea.substring(16,20);
                        String pci_str = packagea.substring(20,24);
                        String rsrp_str = packagea.substring(24,28);
                        String rsrq_str = packagea.substring(28,32);
                        String earfcn_str = packagea.substring(32,40);
                        String cell_id_str = packagea.substring(40,48);
                        String tac_str = packagea.substring(48,52);

                        long device_status = Long.parseLong(device_status_str,16);
                        long smoke_value = Long.parseLong(smoke_value_str,16);
                        long battery_value = Long.parseLong(battery_value_str,16);
                        long temp = Long.parseLong(temp_str,16);
                        if(temp>127) temp = temp-256;
                        long csq = Long.parseLong(csq_str,16);
                        long ecl = Long.parseLong(ecl_str,16);
                        long snr = Long.parseLong(snr_str,16);
                        if(snr>32767) snr = snr-65536;
                        long pci = Long.parseLong(pci_str,16);
                        long rsrp = Long.parseLong(rsrp_str,16);
                        if(rsrp>32767) rsrp = rsrp-65536;
                        long rsrq = Long.parseLong(rsrq_str,16);
                        if(rsrq>32767) rsrq = rsrp-65536;
                        long earfcn = Long.parseLong(earfcn_str,16);
                        long cell_id = Long.parseLong(cell_id_str,16);
                        long tac = Long.parseLong(tac_str,16);
                        DataBean dataBean = new DataBean(device_status,smoke_value,battery_value,temp,csq,ecl,snr,pci,rsrp,rsrq,earfcn,cell_id,tac);
                        onenetTransResultBean.setDataBean(dataBean);

                }else if("1400".equals(type)){
                        onenetTransResultBean.setEventType(2);
                        String packagea = content.substring(10);
                        String mature_asc = packagea.substring(4,8);
                        long dev_type = Long.parseLong(packagea.substring(8,10));
                        String length_module_asc = packagea.substring(10,12);
                        int length_module = Integer.parseInt(length_module_asc,16);
                        String MD_FwVersion_asc = packagea.substring(12,12+length_module*2);
                        String length_app_asc = packagea.substring(12+length_module*2,14+length_module*2);
                        int length_app = Integer.parseInt(length_app_asc,16);
                        String APP_SwVersion_asc = packagea.substring(14+length_module*2,14+length_module*2+length_app*2);

                        //解析得出以下数据
                        String imei_asc = packagea.substring(14+length_module*2+length_app*2,44+length_module*2+length_app*2);
                        String imsi_asc = packagea.substring(44+length_module*2+length_app*2,74+length_module*2+length_app*2);
                        String iccid_asc = packagea.substring(74+length_module*2+length_app*2,114+length_module*2+length_app*2);
                        String ActiveTimeFlag = packagea.substring(114+length_module*2+length_app*2,116+length_module*2+length_app*2);
                        long ActiveTimeFlaglong = Long.parseLong(ActiveTimeFlag,16);
                        String ActiveTime = packagea.substring(116+length_module*2+length_app*2,118+length_module*2+length_app*2);
                        long ActiveTimelong = Long.parseLong(ActiveTime,16);
                        String Tauflag = packagea.substring(118+length_module*2+length_app*2,120+length_module*2+length_app*2);
                        long Tauflaglong = Long.parseLong(Tauflag,16);
                        String Tau = packagea.substring(120+length_module*2+length_app*2,122+length_module*2+length_app*2);
                        long Taulong = Long.parseLong(Tau,16);
                        String mature  = new String(ByteUtls.hexStr2Bytes(mature_asc),"GBK");
                        String MD_FwVersion  = new String(ByteUtls.hexStr2Bytes(MD_FwVersion_asc),"GBK");
                        String APP_SwVersion  = new String(ByteUtls.hexStr2Bytes(APP_SwVersion_asc),"GBK");
                        String imei  = new String(ByteUtls.hexStr2Bytes(imei_asc),"GBK");
                        String imsi  = new String(ByteUtls.hexStr2Bytes(imsi_asc),"GBK");
                        String iccid  = new String(ByteUtls.hexStr2Bytes(iccid_asc),"GBK");
                        ModuleBean moduleBean = new ModuleBean(Tauflaglong,Taulong,mature,APP_SwVersion,MD_FwVersion,dev_type,ActiveTimeFlaglong,ActiveTimelong,imsi,imei,iccid);
                       onenetTransResultBean.setModuleBean(moduleBean);
                }else {
                    onenetTransResultBean.setEventType(0);
                }

        } catch (UnsupportedEncodingException | StringIndexOutOfBoundsException | NumberFormatException e ) {
            e.printStackTrace();
            onenetTransResultBean.setEventType(0);
        }

        return onenetTransResultBean;
    }

    @Override
    public String toString() {
        return "OnetEvent{" +
                "createTime=" + createTime +
                ", eventContent='" + eventContent + '\'' +
                ", deviceId='" + deviceId + '\'' +
                '}';
    }


}
